package com.jhgsys.internal.incentive.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jhgsys.internal.incentive.dto.IncentiveRewardMattersDto;
import com.jhgsys.internal.incentive.entity.IncentiveRewardMatters;
import com.jhgsys.internal.incentive.service.IIncentiveRewardMattersService;
import com.jhgsys.internal.common.annotation.Log;
import com.jhgsys.internal.common.controller.BaseController;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.exception.FebsException;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.dto.TenantDto;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 奖励事项 Controller
 *
 * @author Lujiachen
 * @date 2024-08-09 09:56:24
 */
@Slf4j
@Validated
@Controller
public class IncentiveRewardMattersController extends BaseController {

    @Autowired
    private IIncentiveRewardMattersService incentiveRewardMattersService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardMatters")
    @RequiresPermissions("incentiveRewardMatters:view")
    public String baseProjectIndex(Model model){
        model.addAttribute("tenantList", getTenantList());
        model.addAttribute("tenantId", getCurrentTenantId());
        model.addAttribute("hasHeadCompanyRole", hasHeadCompanyRole());
        return FebsUtil.view("incentive/incentiveRewardMatters/incentiveRewardMattersList");
    }

    /**
     * 查询（分页）
     * @author Lujiachen
     * @date 2024-08-09 09:56:24
     */
    @GetMapping("incentiveRewardMatters/list")
    @ResponseBody
    @RequiresPermissions("incentiveRewardMatters:view")
    public FebsResponse baseProjectList(QueryRequest request, IncentiveRewardMatters incentiveRewardMatters) {
        //没有总公司权限则查询自己公司下的
        if(!hasHeadCompanyRole()){
            incentiveRewardMatters.setTenantId(getCurrentTenantId());
        }
        Map<String, Object> dataTable = getDataTable(this.incentiveRewardMattersService.findIncentiveRewardMatters(request, incentiveRewardMatters));
        return new FebsResponse().success().data(dataTable);
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardMatters/add")
    @RequiresPermissions("incentiveRewardMatters:add")
    public String baseProjectAdd(Model model){
        return FebsUtil.view("incentive/incentiveRewardMatters/incentiveRewardMattersEdit");
    }

    /**
     * 新增 BaseProject
     * @author Lujiachen
     * @date 2024-08-09 11:36:03
     */
    @Log("新增IncentiveRewardMatters")
    @PostMapping("incentiveRewardMatters")
    @ResponseBody
    @RequiresPermissions("incentiveRewardMatters:add")
    public FebsResponse addIncentiveRewardMatters(@RequestBody  IncentiveRewardMatters incentiveRewardMatters) throws FebsException {
        try {
            incentiveRewardMatters.setCreatedBy(getCurrentUser().getUserName());
            this.incentiveRewardMattersService.createIncentiveRewardMatters(incentiveRewardMatters);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "新增奖励事项失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "incentiveRewardMatters/update/{id}")
    @RequiresPermissions("incentiveRewardMatters:update")
    public String incentiveReWardMattersUpdate(Model model, @PathVariable("id") String  id){
        IncentiveRewardMatters incentiveRewardMatters = incentiveRewardMattersService.getOne(new LambdaQueryWrapper<IncentiveRewardMatters>().eq(IncentiveRewardMatters::getRewardMattersId,id));
        model.addAttribute("incentiveRewardMatters", incentiveRewardMatters);
        return FebsUtil.view("incentive/incentiveRewardMatters/incentiveRewardMattersEdit");
    }

    /**
     * 修改 IncentiveRewardMatters
     * @author Lujiachen
     * @date 2024-08-09 15:01:55
     */
    @Log("修改incentiveRewardMatters")
    @PostMapping("incentiveRewardMatters/update")
    @ResponseBody
    @RequiresPermissions("incentiveRewardMatters:update")
    public FebsResponse updateIncentiveRewardMatters(@RequestBody IncentiveRewardMatters incentiveRewardMatters) throws FebsException {
        try {
            incentiveRewardMatters.setUpdatedBy(getCurrentUser().getUserName());
            this.incentiveRewardMattersService.updateBaseArea(incentiveRewardMatters);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改BaseArea失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }

    /**
     * 逻辑删除 IncentiveRewardMatters
     * @author Lujiachen
     * @date 2024-08-09 15:45:47
     */
    @Log("逻辑删除incentiveRewardMatters")
    @GetMapping("incentiveRewardMatters/delete/{id}")
    @ResponseBody
    @RequiresPermissions("incentiveRewardMatters:delete")
    public FebsResponse deleteIncentiveRewardMatters(@PathVariable("id") String rewardMattersId) throws FebsException {
        try {
            IncentiveRewardMatters baseArea = new IncentiveRewardMatters();
            baseArea.setDeletedBy(getCurrentUser().getUserName());
            baseArea.setDeletedTime(new Date());
            baseArea.setDeletedFlg(FebsConstant.INVALID);
            this.incentiveRewardMattersService.deleteIncentiveRewardMatters(baseArea,rewardMattersId);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "删除BaseArea失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }


    /**
     * 修改事项启用状态
     * @param incentiveRewardMatters
     * @return
     * @throws FebsException
     */
    @Log("修改事项启用状态")
    @PostMapping("incentiveRewardMatters/updateStatus")
    @ResponseBody
    @RequiresPermissions(value={"incentiveRewardMatters:update"},logical= Logical.OR)
    public FebsResponse updatePostStatus(IncentiveRewardMatters incentiveRewardMatters) throws FebsException {
        try {

            incentiveRewardMatters.setUpdatedBy(getCurrentUser().getUserName());
            incentiveRewardMatters.setUpdatedTime(new Date());
            this.incentiveRewardMattersService.updateById(incentiveRewardMatters);
            return new FebsResponse().success();
        } catch (Exception e) {
            String message = "修改区域状态失败";
            log.error(message, e);
            throw new FebsException(message);
        }
    }
}
